<template>
  <div class="shoppingcart">
    <div class="header">
      <div class="headertitle">购物车</div>
    </div>
    <van-checkbox-group v-model="result" ref="checkboxGroup">
      <!-- 循环生成的包含van-checkbox的内容 -->
      <div class="content">
        <div class="item" v-for="item in shoppingList" :key="item.id">
          <div class="item-top">
            <van-checkbox
              v-model="item.checked"
              :name="item.id"
              @click="checkone(item)"
              checked-color=" #c1ab96"
            >
            </van-checkbox>
            <div class="delete" @click="del(item.id)">删除</div>
          </div>
          <div class="item-center" @click="getDetail(item.goodsid)">
            <div class="left">
              <img :src="item.src" alt="" />
            </div>
            <div class="right">
              <div class="msg">
                <div class="van-multi-ellipsis--l2">
                  {{ item.goodmsg }}
                </div>
              </div>
              <div class="canshu">单价:￥{{ item.goodprice }}</div>
            </div>
          </div>
          <div class="item-bottom">
            <div class="left"></div>
            <div class="right">
              <div class="price">
                ￥{{ (item.goodprice * item.buynum).toFixed(2) }}
              </div>
              <div class="changenum">
                <span
                  v-show="item.buynum > 1"
                  class="reduce"
                  @click="changenum(item, -1)"
                  >-</span
                >
                <span class="num">{{ item.buynum }}</span>
                <span class="add" @click="changenum(item, 1)">+</span>
              </div>
            </div>
          </div>
        </div>
      </div>
    </van-checkbox-group>

    <!-- <van-submit-bar class="settlement" price="totalprice" button-text="去结算"> -->
    <van-submit-bar
      :price="totalprice * 100"
      button-text="提交订单"
      @submit="onSubmit"
    >
      <van-checkbox
        checked-color=" #c1ab96"
        v-model="isCheckAll"
        @click="setCheckAll"
        >全选</van-checkbox
      >
    </van-submit-bar>
  </div>
</template>
<style lang="scss" scoped>
* {
  margin: 0;
  padding: 0;
}
$main: #c1ab96;
.header {
  padding: 20px;
  // overflow: hidden;
  height: 200px;
  background-color: $main;
  font-size: 20px;
  font-weight: 700;
  color: white;
  .headertitle {
    padding-top: 30px;
  }
}
.content {
  margin-top: -92px;
  margin-bottom: 150px;
  .item {
    background-color: #fff;
    width: 90%;
    height: 192px;
    box-shadow: 0px 2px 10px rgba(51, 51, 51, 0.35);
    margin: auto;
    border-radius: 16px;
    padding: 10px 10px;
    margin-bottom: 20px;
    .item-top {
      display: flex;
      justify-content: space-between;
      margin-bottom: 10px;
      .delete {
        color: tomato;
        padding: 4px;
        border-radius: 4px;
        border: 1px solid tomato;
      }
    }
    .item-center {
      height: 100px;
      // background-color: $main;
      display: flex;
      justify-content: space-around;
      .left {
        width: 100px;
        height: 100%;
        overflow: hidden;
        border-radius: 8px;
        img {
          width: 100%;
          height: 100%;
        }
      }
      .right {
        font-size: 14px;
        width: 170px;
        .msg {
          height: 40px;
        }
        .canshu {
          margin-top: 5px;
          padding-left: 5px;
          height: 50px;
          background-color: rgba(248, 248, 248, 1);
          color: #999999;
          text-align: left;
          line-height: 50px;
        }
      }
    }
    .item-bottom {
      justify-content: space-around;
      display: flex;
      .left {
        width: 100px;
      }
      .right {
        width: 180px;
        line-height: 32px;
        font-size: 14px;
        display: flex;
        justify-content: space-between;
        .price {
          color: red;
        }
        .changenum {
          font-size: 14px;
          .reduce,
          .add,
          .num {
            display: inline-block;
            height: 20px;
            border: 1px solid #999999;
            line-height: 20px;
            width: 20px;
            text-align: center;
          }
          .num {
            width: 35px;
          }
          .reduce {
            margin-right: -1px;
          }
          .add {
            margin-left: -1px;
          }
        }
      }
    }
  }
}
.van-submit-bar {
  box-shadow: 0px 0px 2px 0px rgba(51, 51, 51, 0.35);
  padding: 0 20px 10px 20px;
  position: fixed;
  bottom: 65px;
}
</style>

<script>
// import userModel from "../model/userModel";
import { mapActions, mapState } from "vuex";
import shoppingcartModel from "../model/shoppingcartModel";
export default {
  data() {
    return {
      isCheckAll: false,
      result: [],
      // list: [],
    };
  },

  computed: {
    ...mapState(["shoppingList"]),
    totalprice() {
      let price = 0;
      this.shoppingList.forEach((item) => {
        if (item.ischecked) {
          price += item.buynum * item.goodprice;
        }
      });
      return price.toFixed(2);
    },
  },

  created() {
    this.$store.commit("changeNavFlag", true);

    if (!localStorage.getItem("username")) {
      this.$router.history.push("login");
    }

    this.asyncInitShoppingCartList();

    this.list = this.shoppingList;
    
  },
  methods: {
    ...mapActions(["asyncInitShoppingCartList"]),
    // checkAll() {
    //   this.$refs.checkboxGroup.toggleAll(true);
    // },
    // toggleAll() {
    //   this.$refs.checkboxGroup.toggleAll();
    // },
    getDetail(id) {
      // console.log(11);
      this.$router.history.push({
        name: "detail",
        params: { id },
      });
      // this.$router.go(0);
    },
    checkone(item) {
      if (this.result.length == this.shoppingList.length) {
        this.isCheckAll = true;
      } else {
        this.isCheckAll = false;
      }
      item.ischecked = !item.ischecked;
    },
    changenum(item, num) {
      //item是一个对象  对象是唯一的   也是引用类型 占用同一块内存地址
      item.buynum = item.buynum * 1 + num;
      shoppingcartModel.updata(item.id,{buynum:item.buynum})
        //  shoppingcartModel.updata(item.id, { isbuy: true });
    },
    setCheckAll() {
      if (this.isCheckAll) {
        this.$refs.checkboxGroup.toggleAll(true);
        this.shoppingList.forEach((item) => {
          item.ischecked = true;
        });
      }
      if (!this.isCheckAll) {
        this.$refs.checkboxGroup.toggleAll(false);
        this.shoppingList.forEach((item) => {
          item.ischecked = false;
        });
      }
    },
    onSubmit() {
      if (this.result.length == 0) {
        this.$toast("请选择商品购买");
      } else {
        this.shoppingList.forEach((item) => {
          if (item.ischecked) {
            shoppingcartModel.updata(item.id, { isbuy: true });
          } else {
            shoppingcartModel.updata(item.id, { isbuy: false });
          }
          this.$router.history.push("/buy");
        });
      }
    },
    del(id) {
      shoppingcartModel.del(id);
      // this.asyncInitShoppingCartList();
      this.$router.go(0);
    },
  },

  // beforeDestroy() {
  //   this.$store.commit("changeNavFlag", true);
  // },
};
</script>